package com.hysoft.controller;

import com.hysoft.service.ImportService;
import com.hysoft.util.FileRenameUtil;
import com.hysoft.util.JdbcUtil;
import com.hysoft.util.LogUtil;
import com.hysoft.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;


@Controller
@RequestMapping("/ImportController")
public class ImportController {
    @Autowired
    private ImportService importService;
    @Autowired
    private JdbcUtil jdbcUtil;



    //String host,String port,String SID,String userName,String password,


    @RequestMapping("/findAllAccTable")
    @ResponseBody
    public List<String> findAllAccTable(String path){
        //path="D:\\database1.accdb";
        Connection accessConn = jdbcUtil.getAccessConnection(path);
        System.out.println(accessConn);
        return importService.findAllAccTable(accessConn);
    }

    /**
     *
     * @param host 192.168.0.134
     * @param port 1521
     * @param SID  ORCL
     * @param userName  WL_SCK
     * @param password  WL_SCK
     * @param tablename RYXX_LSCX
     * @param owner       WL_SCK
     * @param
     * @param path
     * @return
     */

    @RequestMapping("/importAccToOracle")
    @ResponseBody
    public Msg importAccToOracle(String host, String port, String SID, String userName, String password, String tablename, String owner, String path){
        String logInfo = "";
        logInfo = LogUtil.getCurrentTime()+LogUtil.getAccessDBInfo("access",path)+LogUtil.getSectionAndAccessPath("",path);
        try {
            LogUtil.inserCorrectLog("In",logInfo,"ok.txt");
            LogUtil.inserCorrectLog("In",logInfo,"error.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Connection accessConn = jdbcUtil.getAccessConnection(path);
        Connection oracleConn=jdbcUtil.getOracleConnection(host,port,SID,userName,password);
        Msg msg = importService.importAccToOracle(oracleConn, accessConn, owner);
        String m = msg.getMsg();
        if (m == "500"){
            String s = FileRenameUtil.renameFile(path);
            LogUtil.InfoWithRename(path,s);
        }
        return msg;
    }
}
